package com.szboanda.parser;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zhangsheng
 * @description: TODO
 * @date ：Created in 2020/2/22 9:55
 * @description：sql解析上下文
 * @modified By：
 * @version: 1.0$
 */
public class SqlContext {

    /**
     * 当前页
     */
    private int pageNum = 1;

    /**
     * 每页的数量
     */
    private int pageSize = 20;

    /**
     * 查询数量上限,默认5w条
     */
    private int queryLimitSize = 50 * 1000;

    /**
     * 分页查询是否需要行号
     */
    private boolean queryNeedRow = false;

    /**
     * 查询超时时间
     */
    private int queryTimeOut = -1;

    /**
     * 数据库类型
     */
    private DataBaseType dataBaseType;

    /**
     * sql解析参数
     */
    private Map<String,Object> params = new HashMap<String,Object>();

    public void put(String key, Boolean val){
        params.put(key, val);
    }

    public void put(String key, Integer val){
        params.put(key, val);
    }

    public void put(String key, String val){
        params.put(key, val);
    }

    public void put(String key, Float val){
        params.put(key, val);
    }

    public void put(String key, Double val){
        params.put(key, val);
    }

    public void put(String key, Date date){
        params.put(key, date);
    }

    public void put(String key, List<?> vals){
        params.put(key, vals);
    }

    public void putAll(Map<String, Object> map) {
        for (Map.Entry<String,Object> entry: map.entrySet()) {
            if (!isAcessType(entry.getValue())) {
                throw new SqlParserException(String.format("%s类型不符合[Boolean,String, Integer, Float, Double, Date, List]", entry.getKey()));
            }
        }
        this.params.putAll(map);
    }

    private boolean isAcessType(Object val) {
        if (val instanceof String || val instanceof Integer || val instanceof Float || val instanceof Double
                || val instanceof Date || val instanceof List || val.getClass().isArray()) {
            return true;
        } else {
            return false;
        }
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public DataBaseType getDataBaseType() {
        return dataBaseType;
    }

    public void setDataBaseType(DataBaseType dataBaseType) {
        this.dataBaseType = dataBaseType;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public int getQueryTimeOut() {
        return queryTimeOut;
    }

    public void setQueryTimeOut(int queryTimeOut) {
        this.queryTimeOut = queryTimeOut;
    }

    public int getQueryLimitSize() {
        return queryLimitSize;
    }

    public void setQueryLimitSize(int queryLimitSize) {
        this.queryLimitSize = queryLimitSize;
    }

    public boolean isQueryNeedRow() {
        return queryNeedRow;
    }

    public void setQueryNeedRow(boolean queryNeedRow) {
        this.queryNeedRow = queryNeedRow;
    }

    @Override
    public String toString() {
        return "[params=" + this.params.toString() + ",pageNum=" + pageNum + ", pageSize= " + pageSize + "]";
    }
}
